import Koa from 'koa';
import Router from 'koa-router';
import bodyparser from 'koa-bodyparser';
import { koaBody } from 'koa-body';
import cors from '@koa/cors';
import { AlipaySdk } from 'alipay-sdk';
const app = new Koa();
const router = new Router();
app.use(cors());
app.use(bodyparser());
app.use(koaBody({ multipart: true }));
app.use(router);
const alipaySdk = new AlipaySdk({
    appId: '9021000137682521',
    // keyType: 'PKCS1', // 默认值。请与生成的密钥格式保持一致，参考平台配置一节
    privateKey: 'MIIEpAIBAAKCAQEA2Y7nP13dEqCKmMyzrLXrWvVa0V55rL2AdyBi2TYBNPZO/kMTvqIcHNJE6Wv4DimXPHjZZXJ8D1FZKHvqr7i2UPjRnwX1BuGHp7PybAccef7NRWBL7VecfDOYL4LH306Bf+sSTem+ji9qmgME+Fp/rCvYC8ReshddrbMSdPZE9m/lvRIHrNxPF5u+dUlHcDJEH16HWplu1MvoJ76oGOJRZFIcacM4yeLPDiNL61ZDZWMvHdR4L78KMfEsskJuSEouNM4o44SwAOdXMHVN1cYOSBqRnh1EuvyCpzarbKsxuKyC/AA1vFtaPnrYt/EIsFy3UHDHSgg7Nag3SCzdoiEReQIDAQABAoIBAQDVPiHh1XH0aRGQdcyDNAkNrcInx++BZnl9ymu5Nhr2u5pv1HWM/D6L7XoIRYDs+M05Q8bqjc2WAglfCh7dwDJLqadltKQX64VY1qN+SXYbCapQHvQtHhvPDY9Uul6Ivd1m8ZWOctcJbOaTMPR1LddIfe0Uk5SyAP8cW5JyvFSaKwHJDAHiNe37/2TqyLBtI+32QzzVf1MQ8a2xdNxfbA/eLROSKF8Ff8g0Qi1E1SwKtYv7Sbd7iwDzMlXSZjsY6rd60xxYxL5PPkHF1/4dz7vQArVA6/UR9+dlm2J9RxTYIdDxTMUITyv/Hh5lo33gnup5Qh9eqA5dcQ6ojchmgGrlAoGBAP7AtwPHQY1+R06UbgXCp/tbXVMT4/rPHElUho4FuG+wLZzXNRYX775M1M9goaQLIv0IexUyhLQ8+gxkCtD5h8EhLukGSQ/+DU3D+E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sskJuSEouNM4o44SwAOdXMHVN1cYOSBqRnh1EuvyCpzarbKsxuKyC/AA1vFtaPnrYt/EIsFy3UHDHSgg7Nag3SCzdoiEReQIDAQAB',
    gateway: "https://openapi-sandbox.dl.alipaydev.com/gateway.do"
});
router.get('/api/zf', async (ctx) => {
    let { id, prices } = ctx.query;
    console.log(ctx.query)
    const bizContent = {
        out_trade_no: id,
        product_code: "FAST_INSTANT_TRADE_PAY",
        subject: '1',
        body: '商品描述',
        total_amount: prices
    }
    // 支付页面接口，返回 html 代码片段，内容为 Form 表单
    const result = alipaySdk.pageExec('alipay.trade.page.pay', {
        method: 'GET',
        bizContent,
        returnUrl: 'https://www.taobao.com'
    });
    ctx.body = {
        code: 200,
        msg: "支付成功",
        result: result
    }
})
app.listen(9006, () => {
    console.log('启动成功');
})